/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.element.ElmQ4;
import br.ufmg.dees.insane.model.disc.element.ElmQ8;
import br.ufmg.dees.insane.model.disc.element.ElmQ9;
import br.ufmg.dees.insane.model.disc.element.ElmT10;
import br.ufmg.dees.insane.model.disc.element.ElmT3;
import br.ufmg.dees.insane.model.disc.element.ElmT6;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.model.disc.shape.Q4;
import br.ufmg.dees.insane.model.disc.shape.Q8;
import br.ufmg.dees.insane.model.disc.shape.Q9;
import br.ufmg.dees.insane.model.disc.shape.Shape;
import br.ufmg.dees.insane.model.disc.shape.T10;
import br.ufmg.dees.insane.model.disc.shape.T3;
import br.ufmg.dees.insane.model.disc.shape.T6;
import br.ufmg.dees.insane.prep.MapElement;
import br.ufmg.dees.insane.prep.MapNode;
import br.ufmg.dees.insane.prep.MappingModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since March 2004
 */
public class DiscretizeCommand implements Command {
    
    private InsaneInternalInterface parent;
	private FemModel fmodel;
	private MappingModel mmodel;
	
//*****************************************************************************
	
    /** Creates new DiscretizeCommand */
    public DiscretizeCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
    }
    
//*****************************************************************************
	
	/** Execute method. */
	public void execute() {
		
		fmodel = parent.getModel().getDiscreteModel();
		fmodel.getNodesList().clear();
		fmodel.getElementsList().clear();
		fmodel.setNumberOfEquations(0);
		fmodel.setNumberOfRestraints(0);
		
		mmodel = (MappingModel)parent.getModel().getMModel();
		Node node;
		Element elm;
		MapNode mn;
		MapElement me;
		
		//Adding nodes to FemModel
		ListIterator nds = mmodel.getMapNodes().listIterator();
		while (nds.hasNext()){
			mn = (MapNode)nds.next();
			node = new Node();
			node.setCoord(mn.getX(), mn.getY());
			node.setLabel(String.valueOf(mn.getLabel()));
			fmodel.add(node);
		}
		
		//Adding elements to FemModel
		ListIterator mes = mmodel.getMapElements().listIterator();
		while (mes.hasNext()){
			
			me = (MapElement)mes.next();
			MapNode[] bound = me.getBoundary();
			MapNode[] connec = me.getConnectivite();
			int numnodes = connec.length;
			
			elm = new FrameElement();
			
			if (me.getType(0)=='Q' && me.getType(1)=='L' && numnodes==4) {
				Shape shp = new Q4();
				fmodel.add(shp);
				elm = new ElmQ4(fmodel.getShape("Q4"));
			} else if (me.getType(0)=='Q' && me.getType(1)=='S' && numnodes==9) {
				Shape shp = new Q9();
				fmodel.add(shp);
				elm = new ElmQ9(fmodel.getShape("Q9"));
			} else if (me.getType(0)=='T' && me.getType(1)=='L' && numnodes==3) {
				Shape shp = new T3();
				fmodel.add(shp);
				elm = new ElmT3(fmodel.getShape("T3"));
			} else if (me.getType(0)=='T' && me.getType(1)=='L' && numnodes==6) {
				Shape shp = new T6();
				fmodel.add(shp);
				elm = new ElmT6(fmodel.getShape("T6"));
			} else if (me.getType(0)=='T' && me.getType(1)=='L' && numnodes==10) {
				Shape shp = new T10();
				fmodel.add(shp);
				elm = new ElmT10(fmodel.getShape("T10"));
			} else if (me.getType(0)=='Q' && me.getType(1)=='S' && numnodes==4) {
				Shape shp = new Q4();
				fmodel.add(shp);
				elm = new ElmQ4(fmodel.getShape("Q4"));
			} else if (me.getType(0)=='Q' && me.getType(1)=='S' && numnodes==8) {
				Shape shp = new Q8();
				fmodel.add(shp);
				elm = new ElmQ8(fmodel.getShape("Q8"));
			} else if (me.getType(0)=='T' && me.getType(1)=='S' && numnodes==3) {
				Shape shp = new T3();
				fmodel.add(shp);
				elm = new ElmT3(fmodel.getShape("T3"));
			} else if (me.getType(0)=='T' && me.getType(1)=='S' && numnodes==6) {
				Shape shp = new T6();
				fmodel.add(shp);
				elm = new ElmT6(fmodel.getShape("T6"));
			}
			
			elm.setLabel( "E" + me.getLabel() );
			fmodel.add(elm);
			
			for (int count = 0; count < bound.length; count++) {
				mn = bound[count];
				Node nd = fmodel.getNode( "" + mn.getLabel() );
				elm.add(nd);
			}
			
			for (int count = 0; count < connec.length; count++) {
				mn = connec[count];
				Node nd = fmodel.getNode( "" + mn.getLabel() );
				if ( elm.getNode( "" + mn.getLabel()) != null) {
					elm.add(nd);
				}
			}
			if (me.getType(0)=='F' && numnodes==2) {
				elm.setAnalysisModel(fmodel.getGlobalAnalysisModel());
				//((FrameElement)elm).setLength();
				((FrameElement)elm).setShape(fmodel);
				elm.setLabel( "E" + ((Node)elm.getIncidence().get(0)).getLabel()+"-"+((Node)elm.getIncidence().get(1)).getLabel());
				
			}
			
		}
		
		if (fmodel.getGlobalAnalysisModel().getType().equals("Grid")) {
			Command cmd = new XY2XZCommand(parent);
			cmd.execute();
		}
		
	}
    
//*****************************************************************************
	
    /**
     * Impossible to undo - not implemented.
     */
    public void undo() {}
    
//*****************************************************************************
	
    /**
     * Impossible to redo - not implemented.
     */
    public void redo() {}
    
//*****************************************************************************
}
